package middlewares

import (
	"fmt"
	"gin-demo/utils/jwt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CheckAuthMiddleware() func(c *gin.Context) {
	return func(c *gin.Context) {
		authHeader := c.Request.Header.Get("token")
		if authHeader != "" {
			fmt.Println("校验token")
			fmt.Println(authHeader)
			mc, err := jwt.ParseToken(authHeader)
			if err != nil {
				c.JSON(http.StatusOK, gin.H{
					"code": 2005,
					"msg":  "无效的token",
				})
				c.Abort()
				return
			}
			c.Set("username", mc.Username)
			c.Next()
		}
	}
}
